/**
 * Created by bootdo.
 */
import axios from 'axios'

axios.defaults.withCredentials = false;
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
//  axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';//配置请求头

let pending = []; //声明一个数组用于存储每个ajax请求的取消函数和ajax标识
let cancelToken = axios.CancelToken;
let removePending = (config) => {
  for(let p in pending){
    if(pending[p].u === config.url + '&' + config.method) { //当当前请求在数组中存在时执行函数体
      pending[p].f(); //执行取消操作
      pending.splice(p, 1); //把这条记录从数组中移除
    }
  }
}

//添加一个请求拦截器
axios.interceptors.request.use(
  config => {
      config.headers.Authorization = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhZG1pbiIsImNvbnRleHRVc2VySWQiOiIwIiwiY29udGV4dE5hbWUiOiLlhpzkuJrlnLDnkIblpKfmlbDmja7lubPlj7DotoXnuqfnrqHnkIblkZgiLCJjb250ZXh0RGVwdElkIjoiMSIsImNvbnRleHRBcHBsaWNhdGlvbklkIjoiMCIsImlhdCI6MTU2MTU5OTM5MCwiZXhwIjoxNTg3NTE5MzkwfQ.QOA1CoOqnpIhbSY9l_ug0V9iG4r03fjYqbr4hiWx0tI";
    return config;
  },
  error => {
    return Promise.reject(error)
  }
);




//通用方法
export const POST = (url, params) => {
  return axios.post(`${url}`, params).then(res => res.data)
};

export const GET = (url, params) => {
  return axios.get(`${url}`, {
    params: params
  }).then(res => res.data)
};
export const POSTBLOB = (url, params) => {
  return axios({
    method:'post',
    url:`${url}`,
    data:params,
    responseType:'blob'
  }).then(res => res.data)
};
export const GETBLOB = (url, params) => {
  return axios.get(`${url}`, {
    params: params,
    responseType:'blob'
  }).then(res => res.data)
};
export const GET2 = (url, params) => {
  return axios.get(`${url}`, {
    params: params
  }).then(res => res.data)
};

export const PUT = (url, params) => {
  return axios.put(`${url}`, params).then(res => res.data)
};

export const DELETE = (url, params) => {
  return axios.delete(`${url}`, {
    params: params
  }).then(res => res.data)
};

export const PATCH = (url, params) => {
  return axios.patch(`${url}`, params).then(res => res.data)
};
